import com from './addon/com'
import schools from './addon/schools'

var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'Btn',
	'Icon',
	'FormGroup',
	'FormInput',
	'FormSelect',
	'FormCode',
	'GridRow'
])

//
new Vue({
	el: '#app',
	components: components,
	created: function(){
		var _this = this;
		schools.getSchools(SITE_URL + '/passport/getschools', function(d){
			_this.schools = d;
		});
	},
	data (){
		return {
			schools: [],
			grades: [],
			classes: [],
			schooltype: '',
			schoolid: ''
		}
	},
	methods: {
		schoolchange: function(obj){
			var _this = this;
			var type = '';
			var sh = this.schools;
			this.grades = [];
			this.classes = [];
			this.schoolid = obj.val;
			this.$refs.grades.setVal('')
			this.$refs.classes.setVal('')
			for(var key=0; key<sh.length; key++){
				if(this.schools[key].val == obj.val){
					type = this.schools[key].type;
					this.schooltype = type;
				}
			}
			schools.getGrades(SITE_URL + '/passport/getgrades', {
				type: type
			}, function(d){
				_this.grades = d;
			});
		},
		gradechange: function(obj){
			var _this = this;
			this.classes = [];
		 
			this.$refs.classes.setVal('')
			schools.getClasses(SITE_URL + '/passport/getclasses', {
				school: _this.schoolid,
				grade: obj.val
			}, function(d){
				_this.classes = d;
			});
		},
		submit: function(){
			this.$broadcast('form-verify', '');
			if(this.$refs.form.pass){

				var data = this.$refs.form.data;

				var req = /^[1-9]{1}\d{0,1}$/;
				if(!req.test(data.sno)){
					bone.dialog.init('', '学号格式错误，必须为小于三位的数字').ok(function(){
						return true;
					});
					return;
				}

				//console.log(this.$refs.form.data);
				$.ajax({
					url: SITE_URL + '/passport/save',
					type:'post',
					data: this.$refs.form.data,
					success: function(data){
						 
						if(!data.code){
							alert(data.msg);
						} else {
							window.location = SITE_URL + '/home'
						}
					},
					error: function(){
						alert('系统错误')
					}
				});
			} else {
				//this.$refs.myform.setNotice('账号密码不正确')
				console.log('data wrong')
			}
		},
		resetsubmit: function(){
	 
			this.$broadcast('form-verify', '');
			if(this.$refs.resetform.pass){
				if(this.$refs.resetform.data.password != this.$refs.resetform.data.password2){
					this.$refs.resetform.setNotice('密码前后输入不一致');
					return;
				}

				$.ajax({
					url: SITE_URL + '/passport/updatepassword',
					type:'post',
					data: this.$refs.resetform.data,
					success: function(data){
						if(!data.code){
							alert(data.msg);
						} else {
							alert(data.msg);
							window.location = SITE_URL + '/passport/index'
						}
					},
					error: function(){
						alert('系统错误')
					}
				});
			} else {
				console.log('wrong data');
			}
		},
		sendCode: function(){
			var ph = this.$refs.phone;
			var code = this.$refs.code;
			ph.verify();
			if(ph.pass){
				ph.enable = true;
				 //
				$.ajax({
					url: SITE_URL + '/passport/sendCode',
					type:'post',
					data: {
						phone: ph.val
					},
					success: function(data){
						if(!data.code){
							alert(data.msg)
						} else {
							code.sendCode();
						}
					},
					error: function(){
						alert('验证码发送失败，请重试')
					}
				});
			}
			 
		}
	}
})